package cn.hj.org.controller;

import cn.hj.basic.util.AjaxResult;
import cn.hj.basic.util.PageList;
import cn.hj.org.domain.Department;
import cn.hj.org.query.DepartmentQuery;
import cn.hj.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
@Api(tags = "部门接口",description = "部门接口详细描述")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 根据Id查询数据
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询",notes = "根据id查询")
    public Department getById(@PathVariable("id") Long id){
        return  departmentService.findById(id);
    }

    /**
     * 查询所有
     * @return
     */
    @GetMapping("/index")
    @ApiOperation(value = "查询所有",notes = "")
    public List<Department> getAll(){
        return departmentService.findAll();
    }

    /**
     * 分页查询加高级查询
     * @param query
     * @return
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询和高级查询",notes = "")
    public PageList<Department> queryData(@RequestBody DepartmentQuery query){
       return  departmentService.selectData(query);
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public AjaxResult  batchRemove(@RequestBody List<Long> ids){
        try {
            departmentService.batchRemove(ids);
            return AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setMessage("删除失败").setSuccess(false);
        }
    }

    /**
     * 根据id删除数据
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除",notes = "根据id删除")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.delete(id);
            return AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.newAjaxResult().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     *新增或修改
     * @param department
     * @return
     */
    @ApiOperation(value = "部门添加或修改",notes = "如果有id就是修改否则就是添加")
    @PostMapping("/save")
    public  AjaxResult save(@RequestBody Department department){
        try {

            //如果id不在存执行新增，不存在执行修改
            if (department.getId() ==null){
                departmentService.insert(department);

            }else {
                departmentService.update(department);
            }
            return  AjaxResult.newAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return   AjaxResult.newAjaxResult().setSuccess(false).setMessage("操作失败");
        }
    }

    /**
     * 查询部门树
     * @return
     */
    @ApiOperation(value = "查询部门树",notes = "")
    @GetMapping("/tree")
    public List<Department> findTree(){
        return departmentService.findTree();
    }

}
